﻿
var Dialog = {

    displayMessage: function( title, content, buttons ) {
        $('.Overlay').show();
        var dialog = $('.Templates .Dialog').clone();
        dialog.find( '.Title' ).append( title );
        dialog.find( '.Content' ).append( content );
        for( var k in buttons ) {
            var button = $('<input type="button" name="' + k + '" value="' + k + '" />');
            button.click( buttons[k] );
            dialog.find( '.Buttons' ).append( button );
        }
        $('.Dialogs').append( dialog );
        return dialog;
    },
    
   displaySimpleMessage: function( title, content ) {
        var o = this.displayMessage( title, content, {
            'Ok' : function() {
                Dialog.close( o );
            }
        } );
        return o;
    },
    
    displayTroopsSender: function( data ) {
        var troops = Math.ceil(Logic.king.academyTroops/2);
        if( data.kingId != Logic.king.id ) {
            troops = Math.min(Logic.king.academyTroops,
             isNaN(data.troops) ? (Logic.globals.initialSquareTroopsNumber + Logic.globals.minimumTroopsToClaimSquare)
             : (data.troops + Logic.globals.minimumTroopsToClaimSquare) );
        }
        
        $('.Actions .SendTroops.Pushed').click();
        var o = this.displayMessage( 'Send troops', 
            $('.Templates .SendTroopsDialog').clone(), 
            { 
                'Send' : function() {
                    var key = data.x + ',' + data.y;
                    data = Map._data[key];
                    if( (data.kingId != Logic.king.id) 
                        && (data.troops > (troops - Logic.globals.minimumTroopsToClaimSquare)) ) {
                        switch( Logic.globals.minimumTroopsToClaimSquare ) {
                            case 0:
                                if( !confirm( 'You need to have killed all troops'
                                    + ' at the end of the battle top claim square. Are you sure you want to continue ?' ) ) {
                                    return;
                                }
                                break;
                            case 1:
                                if( !confirm( 'You need to have 1 troop'
                                    + ' at the end of the battle top claim square. Are you sure you want to continue ?' ) ) {
                                    return;
                                }
                                break; 
                            default:
                                if( !confirm( 'You need to have ' + Logic.globals.minimumTroopsToClaimSquare + ' troops'
                                    + ' at the end of the battle top claim square. Are you sure you want to continue ?' ) ) {
                                    return;
                                }
                                break;
                        }
                    }
                    $.post( '/Game/SendTroops', {
                        x : data.x,
                        y : data.y,
                        sessionId : Logic.globals.sessionId,
                        troops : troops
                    }, function( json ) {
                        Logic.handleSynchronization( json );
                        Map.handleSynchronization( json );
                        if( !json.Success ) {
                            Dialog.displaySimpleMessage( 'Movement failed',
                                "<p>King can't send troops to this destination</p>" );
                        }
                    }, 'json' );
                    Dialog.close( o );
                },
                
                'Cancel' : function() {
                    Dialog.close( o );
                }
            } );
         
        $('.TroopsNumber', o).attr( 'value', troops );
        $('.TroopsNumber', o).bind( 'change keyup blur', function() {
            var value = $(this).attr( 'value' );
            var flag = value;
            value = value.replace( /[^0-9]/g, '' );
            if( value != flag ) {
                $(this).attr( 'value', value );
            }
            if( value == '' ) {
                $('input[type=button]:first', o).attr( 'disabled', 'disabled' );
                return;
            }
            value = parseInt(value);
            if( value > Logic.king.academyTroops ) {
                $('input[type=button]:first', o).attr( 'disabled', 'disabled' );
                return;
            }
            $('input[type=button]:first', o).removeAttr( 'disabled' );
            troops = value;
            $('.Slider', o).slider( 'value', value );
        } );
            
        $('.Slider', o).slider( {
            min : 1,
            value : troops,
            max : Logic.king.academyTroops,
            step : 1,
            slide: function(e, ui) {
                troops = ui.value;
                $('.TroopsNumber', o).attr( 'value', ui.value );
            }
        } );
        
        $('input[type=text]', o).select();
        return o;
    },
    
    displayTroopsTaker: function() {
    
        var data = Map._kingData;
        var max = data.troops - Logic.king.academyTroops;
        var troops = Math.ceil(max/2);
        
        var o = this.displayMessage( 'Take troops', 
            $('.Templates .TakeTroopsDialog').clone(), 
            { 
                'Take' : function() {
                    var key = data.x + ',' + data.y;
                    data = Map._data[key];
                    
                    $.post( '/Game/TakeTroops', {
                        sessionId : Logic.globals.sessionId,
                        troops : troops
                    }, function( json ) {
                        Logic.handleSynchronization( json );
                        Map.handleSynchronization( json );
                        if( !json.Success ) {
                            Dialog.displaySimpleMessage( 'Troops failed to be taken',
                                "<p>King can't take troops</p>" );
                        }
                    }, 'json' );
                    Dialog.close( o );
                },
                
                'Cancel' : function() {
                    Dialog.close( o );
                }
            } );
         
        $('.TroopsNumber', o).attr( 'value', troops );
        $('.TroopsNumber', o).bind( 'change keyup blur', function() {
            var value = $(this).attr( 'value' );
            var flag = value;
            value = value.replace( /[^0-9]/g, '' );
            if( value != flag ) {
                $(this).attr( 'value', value );
            }
            if( value == '' ) {
                $('input[type=button]:first', o).attr( 'disabled', 'disabled' );
                return;
            }
            value = parseInt(value);
            if( value > max ) {
                $('input[type=button]:first', o).attr( 'disabled', 'disabled' );
                return;
            }
            $('input[type=button]:first', o).removeAttr( 'disabled' );
            troops = value;
            $('.Slider', o).slider( 'value', value );
        } );
            
        $('.Slider', o).slider( {
            min : 1,
            value : troops,
            max : max,
            step : 1,
            slide: function(e, ui) {
                troops = ui.value;
                $('.TroopsNumber', o).attr( 'value', ui.value );
            }
        } );
        
        $('input[type=text]', o).select();
        return o;
    },
    
    displayMoveKingConfirmation: function( data ) {
        var o = this.displayMessage( 'Move king', 
            '<p>Are you sure you want to move king ?</p>', 
            {
                'Yes' : function() {
                    var pos = Logic.king.position;
                    var direction;
                    if( data.x == pos.x + 1 ) {
                        direction = 'EAST';
                    }
                    else if( data.x == pos.x - 1) {
                        direction = 'WEST';
                    }
                    else if( data.y == pos.y - 1) {
                        direction = 'NORTH';
                    }
                    else if( data.y == pos.y + 1 ) {
                        direction = 'SOUTH';
                    }
                   
                    $.post( '/Game/MoveKing', {
                        direction: direction,
                        sessionId : Logic.globals.sessionId
                    }, function( json ) {
                        Logic.handleSynchronization( json );
                        Map.handleSynchronization( json );
                        if( !json.Success ) {
                            Dialog.displaySimpleMessage( 'Movement failed',
                                "<p>King can't move to this destination</p>" );
                        }
                    }, 'json' );
                    Dialog.close( o );
                },
                
                'Cancel' : function() {
                    Dialog.close( o );
                }
            } );
        
        return o;
    },
    
    close: function( dialog ) {
        $('.Overlay').hide();
        $(dialog).remove();
    }

}